<?
$sub_menu = "400300";
include_once("./_common.php");
include_once("$g4[path]/lib/shop.lib.php");
auth_check($auth[$sub_menu], "r");
$g4[title] = "주문 관리";
include_once("./admin.head.php");

if(!$startDate || $startDate == ""){$startDate = date("Ymd");}
if(!$endDate || $endDate == ""){$endDate = date("Ymd");}

$tmp_startDate = substr($startDate,0,4)."-".substr($startDate,4,2)."-".substr($startDate,6,2);
$tmp_endDate = substr($endDate,0,4)."-".substr($endDate,4,2)."-".substr($endDate,6,2);

$beTween = " and (buy_datetime >= '".$tmp_startDate." 00:00:00' and buy_datetime <= '".$tmp_endDate." 23:59:59') ";

if($status)
{
	$status_search = " and status = '".substr($status,1,1)."' ";
}

if ($stx)
{
    $sql_search = " and ( ";
    $sql_search .= " concat(o_name,s_name,mb_id,o_subject) like '%$stx%' ";
    $sql_search .= " )";
}



$sql = "select * from g4_order where 1 $beTween $sql_search $status_search";

$total_array = mysql_fetch_array(mysql_query(str_replace("*","sum(pay_price) as tot_price,sum(pay_tranc) as tot_tranc,sum(pay_point) as tot_point,sum(pay_amount) as tot_amount",$sql)));

if (!$sst) {
    $sst  = "idx";
    $sod = "desc";
}
$sql.= " order by $sst $sod ";

$total_count = mysql_num_rows(mysql_query($sql));

$rows = 10;
$total_page  = ceil($total_count / $rows);  // 전체 페이지 계산
if ($page == "") { $page = 1; } // 페이지가 없으면 첫 페이지 (1 페이지)
$from_record = ($page - 1) * $rows; // 시작 열을 구함

$excel_sql = $sql;

$sql.=" limit $from_record, $rows";
$rst = mysql_query($sql);

$RowNum = $total_count - $from_record;

$row_count = mysql_num_rows(mysql_query($sql));
$pagelist = get_paging("5", $page, $total_page, "$_SERVER[PHP_SELF]?status=$status&startDate=$startDate&endDate=$endDate$qstr&page=");

$colspan = "10";

?>
<table cellpadding="0" cellspacing="1" border="0" width="100%">
	<colgroup>
		<col width="20">
		<col width="50">
		<col width="120">
		<col width="">
		<col width="100">
		<col width="120">
		<col width="80">
		<col width="80">
		<col width="100">
		<col width="60">
	</colgroup>
	<tr>
		<form name="excel_frm" method="post" action="order_excel.php">
		<input type="hidden" name="excel_sql" value="<?=$excel_sql?>">
		<td colspan="3"><button class="btn1" style="width:100px;cursor:point" onclick="if(confirm('엑셀로 저장하시겠습니까?')){submit();}">엑셀저장</button></td>
		</form>
		<form name=fsearch method=get>
		<td colspan="7" align="right">
		검색기간 : 
		<input type="text" class="ed" style="width:65px" name="startDate" readonly value="<?=$startDate?>" id="mb_start" onclick="win_calendar('mb_start', document.getElementById('mb_start').value, '');">
		~
		<input type="text" class="ed" style="width:65px" name="endDate" readonly value="<?=$endDate?>" id="mb_end" onclick="win_calendar('mb_end', document.getElementById('mb_end').value, '');">
		상태 : 
		<select name="status">
			<option value="">전체</option>
			<?
			for($i=0;$i<=8;$i++)
			{
			?>
			<option value="<?="S".$i?>" <?if("S".$i == $status){echo("selected");}?>><?=get_status($i);?></option>
			<?}?>
		</select>
		검색 : 
		<input type="text" name="stx" class="ed" style="width:120px" value="<?=$stx?>">
		<button class="btn1" style="cursor:pointer;height:20px;width:45px" onclick="submit();">검색</button>
		</td>
		</form>
	</tr>
	<tr height="25" bgcolor="EFEFEF" align="center">
		<td><input type="checkbox" onclick="CheckAll(this)"></td>
		<td>순번</td>
		<td><?=subject_sort_link("oid","status=$status&startDate=$startDate&endDate=$endDate")?>주문코드</a></td>
		<td><?=subject_sort_link("o_subject","status=$status&startDate=$startDate&endDate=$endDate")?>상품명</a></td>
		<td><?=subject_sort_link("o_name","status=$status&startDate=$startDate&endDate=$endDate")?>주문자명</a></td>
		<td><?=subject_sort_link("pay_amount","status=$status&startDate=$startDate&endDate=$endDate")?>결제금액</a></td>
		<td><?=subject_sort_link("pay_method","status=$status&startDate=$startDate&endDate=$endDate")?>결제방법</a></td>
		<td><?=subject_sort_link("status","status=$status&startDate=$startDate&endDate=$endDate")?>상태</a></td>
		<td><?=subject_sort_link("buy_datetime","status=$status&startDate=$startDate&endDate=$endDate")?>주문일시</a></td>
		<td></td>
	</tr>
	<form name="sAll" method="post" action="order_list_update.php">
	<input type="hidden" name="Mode" value="STATUS">
	<?
	$row_no = 1;
	while($row = mysql_fetch_array($rst))
	{
	?>
	<tr height="50" align="center" onmouseover="this.style.background='#CCFFFF'" onmouseout="this.style.background='#FFFFFF'">
		<td><input type="checkbox" id="sidx_<?=$row_no?>" name="sidx[]" value="<?=$row[idx]?>"></td>
		<td><?=number_format($RowNum,0)?></td>
		<td><a href="javascript:ViewOrder('<?=$row[oid]?>')"><?=$row[oid]?></a></td>
		<td align="left"><?=cut_str($row[o_subject],60)?></td>
		<td>
		<?=$row[o_name]?>
		<?if($row[mb_id]!=""){?><br>(<a href="member_form.php?&w=u&mb_id=<?=$row[mb_id]?>"><?=$row[mb_id]?></a>)<?}?>
		</td>
		<td align="right">
			<table cellpadding="0" cellspacing="0" border="0" width="100%">
				<tr bgcolor="FFCCCC">
					<td>상품총액</td>
					<td align="right"><?=number_format($row[pay_price])?>원</td>
				</tr>
				<tr bgcolor="FFFFCC">
					<td>배송비</td>
					<td align="right"><?=number_format($row[pay_tranc])?>원</td>
				</tr>
				<tr bgcolor="CCFFCC">
					<td>포인트결제</td>
					<td align="right"><?=number_format($row[pay_point])?>원</td>
				</tr>
				<tr bgcolor="CCCCFF">
					<td>결제총액</td>
					<td align="right"><?=number_format($row[pay_amount])?>원</td>
				</tr>
			</table>
		</td>
		<td><?=get_paymethod($row[pay_method])?></td>
		<td onmouseover="document.all('<?=$row[oid]?>').style.display='inline';"  onmouseout="document.all('<?=$row[oid]?>').style.display='none';">
		<?=get_status($row[status])?>
		<DIV style="position:absolute;display:none;" id="<?=$row[oid]?>">
		<table cellpadding="5" cellspacing="1" border="0" width="250" bgcolor="#CCCCCC" style="position:absolute;left:-150px;top:+20px">
		<?
		$tmp_status_history = explode("/",$row[status_history]);
		for($i=0;$i<count($tmp_status_history);$i++)
		{
			$tmp_history = explode("|",$tmp_status_history[$i]);
			?>
			<tr bgcolor="#FFFFFF">
				<td><?=number_format($i+1)?></td>
				<td><?=get_status($tmp_history[0]);?></td>
				<td><?=$tmp_history[1]?></td>
			</tr>
			<?
		}
		?>
		</table>
		</DIV>
		</td>
		<td><?=str_replace(" ","<br>",$row[buy_datetime])?></td>
		<td>
		<img src='<?=$g4[admin_path]?>/img/icon_view.gif' border=0 title='보기' style="cursor:pointer" onclick="ViewOrder('<?=$row[oid]?>')">
		<img src='<?=$g4[admin_path]?>/img/icon_delete.gif' border=0 title='삭제' style="cursor:pointer" onclick="if(confirm('삭제 하시겠습니까?\n\n삭제된 데이터는 복구되지 않습니다.')){location.href='./order_list_update.php?Mode=DEL&IDX=<?=$row[idx]?>';}">
		</td>
	</tr>
	<tr><td colspan="<?=$colspan?>" height="1" bgcolor="#d7d7d7"></td></tr>
	<?
		$RowNum--;
		$row_no++;
	}
	?>
	<tr><td colspan="<?=$colspan?>" height="40" align="right"><?=$pagelist?></td></tr>
	<tr>
		<td colspan="<?=$colspan?>" height="40" align="left">
		상태 : 
		<select name="list_status">
			<option value="">선택</option>
		<?
		for($j=0;$j<=8;$j++)
		{
		?>
			<option value="<?=$j?>"><?=get_status($j)?></option>
		<?
		}
		?>
		</select>
		<button class="btn1" onclick="sCheck();">선택수정</button>
		</td>
	</tr>
	</form>
</table>
<br>
<table cellpadding="0" cellspacing="0" border="0" style="border:solid 1px #D7D7D7">
	<tr height="25">
		<td width="100" bgcolor="EFEFEF" align="center">상품총액</td>
		<td width="100" align="right"><?=number_format($total_array[tot_price])?>원</td>
		<td width="100" bgcolor="EFEFEF" align="center">배송비총액</td>
		<td width="100" align="right"><?=number_format($total_array[tot_tranc])?>원</td>
		<td width="100" bgcolor="EFEFEF" align="center">포인트총액</td>
		<td width="100" align="right"><?=number_format($total_array[tot_point])?>원</td>
		<td width="100" bgcolor="EFEFEF" align="center">결제총액</td>
		<td width="100" align="right"><?=number_format($total_array[tot_amount])?>원</td>
	</tr>
</table>
<script>
function CheckAll(Obj)
{
	for(i=1;i<=<?=$row_count?>;i++)
	{
		if(Obj.checked == true)
		{
			document.getElementById("sidx_"+i).checked = true;
		}
		else
		{
			document.getElementById("sidx_"+i).checked = false;
		}
	}
}
function sCheck()
{
	var frm = document.sAll;
	var continu = false;
	for(i=1;i<=<?=$row_count?>;i++)
	{
		if(document.getElementById("sidx_"+i).checked == true)
		{
			continu = true;
			break;
		}
	}

	if(continu == true)
	{
		if(frm.list_status.value == "")
		{
			alert("변경하실 상태를 선택하세요.");
			frm.list_status.focus();
		}
		else
		{
			if(confirm("선택하신 주문의 상태를 변경하시겠습니까?"))
			{
				frm.submit();
			}
			else
			{
				return false;
			}
		}
	}
	else
	{
		alert("선택하세요.");
		return false;
	}
}

function ViewOrder(OID)
{
	var URL = "./order_view.php?OID="+OID;
	var NAME = "";
	var OPTION = "width=720,height=500,scrollbars=yes,resizable=no";
	window.open(URL,NAME,OPTION);
}
</script>
<?
include_once ("./admin.tail.php");
?>